package simbot.example.demo.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * Created by henery on 2017/9/21.
 */
@Component
public class JsonPriceSerializer extends JsonSerializer<Long> {
    @Override
    public void serialize(Long price, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
        StringBuilder formattedPrice = new StringBuilder("");
        long yuan = price / 100;
        long fen = price % 100;

        formattedPrice.append(yuan).append(".");
        if (fen < 10) {
            formattedPrice.append("0");
        }
        formattedPrice.append(fen);

        jsonGenerator.writeString(formattedPrice.toString());
    }
}